﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using EC_Agent.Models;
using EC_Agent.AgentClass;

namespace EC_Agent
{
    public partial class adDetailKhachHang : System.Web.UI.Page
    {
        KhachHangInfo khachhang = new KhachHangInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadChiTietKH();
        }

        public void LoadChiTietKH()
        {
            String key = Request.QueryString["ID"];
            DataSet ds = new KhachHangModels().ListKhachHang();
            DataRow[] dw = ds.Tables[0].Select(" MaKhachHang = " + key);
            lbUsername.Text = dw[0]["Username"].ToString();
            lbTenKH.Text = dw[0]["TenKH"].ToString();
            lbDiaChi.Text = dw[0]["DiaChi"].ToString();
            lbEmail.Text = dw[0]["Email"].ToString();
            lbDienThoai.Text = dw[0]["DienThoai"].ToString();
            DateTime dt = Convert.ToDateTime(dw[0]["NgaySinh"].ToString());
            lbNgaySinh.Text = dt.Day + "/" + dt.Month + "/" + dt.Year;
            lbSoTheTinDung.Text = dw[0]["SoTheTinDung"].ToString();
            Boolean tt = Convert.ToBoolean(dw[0]["TinhTrang"].ToString());
            if (tt)
                lbTinhTrang.Text = "Đã kích hoạt";
            else lbTinhTrang.Text = "Vô hiệu hóa";
            khachhang.MaKhachHang = Convert.ToInt16(key);
            khachhang.DiaChi = dw[0]["DiaChi"].ToString();
            khachhang.DienThoai = dw[0]["DienThoai"].ToString();
            khachhang.Email = dw[0]["Email"].ToString();
            khachhang.NgaySinh = Convert.ToDateTime(dw[0]["NgaySinh"].ToString());
            khachhang.TenKH = dw[0]["TenKH"].ToString();
            khachhang.SoTheTinDung = dw[0]["SoTheTinDung"].ToString();
            khachhang.TinhTrang = Convert.ToBoolean(dw[0]["TinhTrang"].ToString());
        }


        static string GetMd5Hash(MD5 md5Hash, string input)
        {

            // Convert the input string to a byte array and compute the hash. 
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes 
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data  
            // and format each one as a hexadecimal string. 
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string. 
            return sBuilder.ToString();
        }

        protected void UpdatePass_Click(object sender, EventArgs e)
        {
            if (txtPassword.Text == txtConfirmPassword.Text)
            {
                tabUpdatePass.Attributes.Add("style", "display:none");
                txtConfirmPassword.Text = "";
                txtPassword.Text = "";
                khachhang.Password = txtPassword.Text;
                MD5 md5Hash = MD5.Create();
                string hash = GetMd5Hash(md5Hash, txtPassword.Text);
                khachhang.Password = hash;
                new KhachHangModels().UpdateMKKhachHang(khachhang);
                MessageBox.Show("asas");
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            tabUpdatePass.Attributes.Add("style", "display:block");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int year;
            for (year = DateTime.Now.Year - 100; year <= DateTime.Now.Year - 18; year++)
            {
                ddlYear.Items.Add(year.ToString());
            }
            txtTenKH.Text = khachhang.TenKH;
            txtDiaChi.Text = khachhang.DiaChi;
            txtDienThoai.Text = khachhang.DienThoai;
            txtEmail.Text = khachhang.Email;
            txtSoTheTinDung.Text = khachhang.SoTheTinDung;
            ddlDay.Text = khachhang.NgaySinh.Day.ToString();
            ddlMonth.SelectedValue = khachhang.NgaySinh.Month.ToString();
            ddlYear.Text = khachhang.NgaySinh.Year.ToString();
            Detail.Attributes.Add("style", "display:none");
            tabUpdate.Attributes.Add("style", "display:block");
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            tabUpdatePass.Attributes.Add("style", "display:none");
            txtConfirmPassword.Text = "";
            txtPassword.Text = "";
        }

        protected void btnCapNhatTT_Click(object sender, EventArgs e)
        {
            KhachHangInfo khachhang1 = new KhachHangInfo();
            khachhang1.MaKhachHang = khachhang.MaKhachHang;
            khachhang1.TenKH = txtTenKH.Text;
            khachhang1.DiaChi = txtDiaChi.Text;
            khachhang1.DienThoai = txtDienThoai.Text;
            khachhang1.Email = txtEmail.Text;
            khachhang1.NgaySinh = new DateTime(Convert.ToInt16(ddlYear.Text), Convert.ToInt16(ddlMonth.SelectedValue.ToString()), Convert.ToInt16(ddlDay.Text));
            khachhang1.SoTheTinDung = txtSoTheTinDung.Text;
            khachhang1.TinhTrang = chkTinhTrang.Checked;
            new KhachHangModels().UpdateKhachHang(khachhang1);
            Detail.Attributes.Add("style", "display:block");
            tabUpdate.Attributes.Add("style", "display:none");
            LoadChiTietKH();
        }

        protected void btnCancelTT_Click(object sender, EventArgs e)
        {
            Detail.Attributes.Add("style", "display:block");
            tabUpdate.Attributes.Add("style", "display:none");
        }
    }
}